﻿using DevComponents.AdvTree;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class StrengthGradeFormCfg : XtraForm
    {
        private List<string> m_StrengthGradeList = new List<string>();

        public StrengthGradeFormCfg()
        {
            InitializeComponent();
        }

        private void StrengthGradeFormCfg_Load(object sender, EventArgs e)
        {
            if(!RefreshStrengthGrade())
                MessageBox.Show(this, "刷新强度等级信息异常");
        }

        private bool RefreshStrengthGrade()
        {
            try
            {
                RootNode.Nodes.Clear();
                string strengthGrade = ConfigurationManager.AppSettings["StrengthGrade"];
                if (!string.IsNullOrEmpty(strengthGrade))
                {
                    foreach (string name in strengthGrade.Split(','))
                    {
                        if (string.IsNullOrEmpty(name))
                            continue;
                        if (!m_StrengthGradeList.Contains(name))
                            m_StrengthGradeList.Add(name);
                        Node node = new Node();
                        node.Text = name;
                        RootNode.Nodes.Add(node);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("加载强度等级异常：" + ex.ToString());
                return false;
            }
            return true;
        }

        private void btnDriverAdd_Click(object sender, EventArgs e)
        {
            ActionForm form = new ActionForm();
            form.IsAdd = true;
            form.NameList = m_StrengthGradeList;
            if (DialogResult.OK != form.ShowDialog())
                return;
            string name = form.Name;
            if (!m_StrengthGradeList.Contains(name))
                m_StrengthGradeList.Add(name);
            string str = "";
            foreach (string strengthGrade in m_StrengthGradeList)
                str += strengthGrade + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["StrengthGrade"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshStrengthGrade();
        }

        private void btnDriverModify_Click(object sender, EventArgs e)
        {
            Node selectedNode = advTreeDriver.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要修改的名称");
                return;
            }
            ActionForm form = new ActionForm();
            form.IsAdd = false;
            form.Name = selectedNode.Text;
            form.NameList = m_StrengthGradeList;
            if (DialogResult.OK != form.ShowDialog())
                return;
            string name = form.NewName;
            m_StrengthGradeList.Remove(selectedNode.Text);
            if (!m_StrengthGradeList.Contains(name))
                m_StrengthGradeList.Add(name);
            string str = "";
            foreach (string strengthGrade in m_StrengthGradeList)
                str += strengthGrade + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["StrengthGrade"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshStrengthGrade();
        }

        private void btnDriverDel_Click(object sender, EventArgs e)
        {
            Node selectedNode = advTreeDriver.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要删除的名称");
                return;
            }
            m_StrengthGradeList.Remove(selectedNode.Text);
            string str = "";
            foreach (string strengthGrade in m_StrengthGradeList)
                str += strengthGrade + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["StrengthGrade"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshStrengthGrade();
        }
    }
}
